import * as Cesium from 'cesium'

export default class MousePosition {
  constructor(viewer, positionLabel) {
    // 监听鼠标移动时间
    const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)

    handler.setInputAction(movement => {
      // 获取鼠标的坐标
      const cartesian = viewer.camera.pickEllipsoid(
        movement.endPosition,
        viewer.scene.globe.ellipsoid,
      )
      // 转换经纬度
      if (cartesian) {
        const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
        const longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(5)
        const latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(5)
        // const heightString = cartographic.height
        positionLabel.value = `经度：${longitudeString} 纬度：${latitudeString}`
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
  }
}
